using System.Collections.Generic;
using UnityEngine;

namespace Framework
{
    public class Bezier
    {
        private static List<Vector3> _list = new List<Vector3>();
        public static Vector3 BezierCurve(Vector3 P0, Vector3 P1, Vector3 P2, float t)
        {
            float t1 = (1 - t) * (1 - t);
            float t2 = t * (1 - t);
            float t3 = t * t;
            Vector3 B = P0 * t1 + 2 * t2 * P1 + t3 * P2;
            return B;
        }
        public static Vector3 BezierCurve(Vector3 P0, Vector3 P1, Vector3 P2, Vector3 P3, float t)
        {
            float t1 = (1 - t) * (1 - t) * (1 - t);
            float t2 = (1 - t) * (1 - t) * t;
            float t3 = t * t * (1 - t);
            float t4 = t * t * t;
            Vector3 B = P0 * t1 + 3 * t2 * P1 + 3 * t3 * P2 + P3 * t4;
            return B;
        }
        public static Vector3 BezierCurve(ICollection<Vector3> list, float t)
        {
            _list.Clear();
            _list.AddRange(list);
            while (true)
            {
                var len = _list.Count;
                if (2 > len)
                    return _list[0];
                for (var i = 0; i < len - 1; ++i)
                {
                    var p0 = _list[i];
                    var p1 = _list[i + 1];
                    _list[i] = (1 - t) * p0 + t * p1;
                }
                _list.RemoveAt(len - 1);
            }
        }
    }
}
